from datetime import datetime

from sqlalchemy import ForeignKey

from exts import db


class UserModel(db.Model):
    __tablename__='user'
    id=db.Column(db.Integer,primary_key=True,autoincrement=True)
    username=db.Column(db.String(100),nullable=False)
    password=db.Column(db.String(254),nullable=False)
    email=db.Column(db.String(100),nullable=False,unique=True)
    join_time=db.Column(db.DateTime,default=datetime.now)


class EmailCode(db.Model):
    __tablename__='email_code'
    id=db.Column(db.Integer,primary_key=True,autoincrement=True)
    code=db.Column(db.String(100),nullable=False)
    email=db.Column(db.String(100),nullable=False)

class Question(db.Model):
    __tablename__='question'
    id=db.Column(db.Integer,primary_key=True,autoincrement=True)
    title=db.Column(db.String(100),nullable=False)
    content=db.Column(db.Text, nullable=False)
    create_time=db.Column(db.DateTime,default=datetime.now)
    qusetion_setter_id=db.Column(db.Integer,ForeignKey('user.id'))
    qusetion_setter=db.relationship(UserModel,backref='qusetion')

class Answer(db.Model):
    __tablename__='answer'
    id=db.Column(db.Integer,primary_key=True,autoincrement=True)
    content=db.Column(db.Text, nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)
    question_id=db.Column(db.Integer,ForeignKey('question.id'))
    answer_setter_id=db.Column(db.Integer,ForeignKey('user.id'))

    qusetion=db.relationship(Question,backref=db.backref('answers',order_by=create_time.desc))
    answer_setterr=db.relationship(UserModel,backref='answer')